<?php
	//include 'includes/functions.php';

	/* forced url protect */
	if(curPageName() != 'index.php') header('location:index.php');
	$_SESSION['msg'] = '';

	/* ***********************************************************************
     *      LOGIN 
     * ***********************************************************************/
	if(isset($_POST['signup']))
	{
		/* check for dupes */
		$_SESSION['msg'] = '';																/* clear err msg 			*/
		$a = $_POST;																		/* simplify var 			*/
		if (select('account', 'username', $a['username']) > 0)								/* dupe username??			*/
			$_SESSION['msg'] .= '<p class="error_msg">Account name is not available</p>';
		if (select('account', 'email', $a['email']) > 0)									/* dupe email 				*/
			$_SESSION['msg'] .= '<p class="error_msg">Unusable email</p>';
		if($a['password'] != $a['password'])												/* Password 				*/
			$_SESSION['msg'] .= '<p class="error_msg">Passwords do not match</p>';

		/* ***********************************************************************
     	 *      ALL CLEAR ADD TO DATABASE
     	 * ***********************************************************************/
		if($_SESSION['msg'] == '')															/*  if msg is still null?	*/
		{
			$a['username'] = strtoupper($a['username']);									/* uppercase names for pw 	*/
			$a['password'] = md5(strtoupper($a['username'].':'.$a['password']));			/* mangos style pw 			*/
			if(createAccount($a))															/* create account 			*/
				$_SESSION['msg'] .= '
					<p class="success">
						<p>Account created successfully. You can now login.</p>
						<p><a href="index.php">Click here</a> to continue...</p>
					</p>';
			else
				$_SESSION['msg'] .= '<p class="error_msg">Unable to create Account</p>';
		}
	}// end post signup

?>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="scripts/troll.css"/>
    <title>TROLL: Signup</title>
</head>
<body>
    <center>
    <div class="console">        
        <table width="800" clas="console">
            <tr>
                <td>                
                    <center>
                    <img src='images/troll.bmp'/><br/>
                    <h1 class="title">TROLL : Signup</h1>
                    <?php if(isset($_SESSION['msg']))
                    		{
                    			//STUPD: FIX LATER
                    			if(strpos($_SESSION['msg'],"success"))
                    			{
                    				echo $_SESSION['msg'];
                    				die();
                    			}
                    			else
                    			{
                					 echo $_SESSION['msg'];
                    			}
                    		}
                    ?>
                    <form method="post" action="index.php?signup">
                    	<table>
                    		<tr><td><input type="text" name="username" placeholder="Username" required autofocus/></td></tr>
                    		<tr><td><input type="password" name="password" placeholder="Password" required/></td></tr>
                    		<tr><td><input type="password" name="pw2" placeholder="Re-Enter PW" required/></td></tr>
                    		<tr><td><input type="email" name="email" placeholder="Valid Email" required/></td></tr>
            				<tr><td colspan="2"><p><input type="submit" vale="Signup" name="signup"/></p></td></tr>

                    </form>
                    </center>
                 </td>
            </tr>
        </table>
    </div>
    </center>
</body>
</html>
